package com.lsnu.wzy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lsnu.wzy.po.Doctor;
import com.lsnu.wzy.po.Patient;
import com.lsnu.wzy.service.PatientService;

@RestController
@RequestMapping("/patientOption")
public class PatientController {
	@Autowired
	private  PatientService patientService;

	//按照id查询患者
	@RequestMapping("/findById/{pID}")
	public ModelAndView FindById(String pID)
	{
		List<Patient> list = new ArrayList<Patient>();
		Patient patient= patientService.findPatient(pID); 
		list.add(patient);
		ModelAndView mav=new ModelAndView("/jsp/pages/patients-list.jsp");
		mav.addObject("patient", list);
		mav.addObject("pID",pID);
		return mav;		
	}
	
	//管理员点击添加事件
	@RequestMapping("/beforeADD2")
	public ModelAndView beforeADD2()
	{
		Patient patient=patientService.findLast();
		String pID=patient.getpID();
		char[] arr=pID.toCharArray();
		StringBuilder builder = new StringBuilder();
	        for (int i = 0; i < arr.length; i++) {
//	        	如果字符的ACSLL码在48-57的范围内那肯定就是整数了
				if(arr[i] >= 48 && arr[i] <= 57) {
					builder.append(arr[i]);
				}
			}
	    Integer integer=(Integer.parseInt(builder.toString()))+1;
		String pString="P0"+integer;
		ModelAndView mav=new ModelAndView("/jsp/pages/addPatient.jsp");
		mav.addObject("pID",pString);
		return mav;
	}
	
	//添加患者
	@RequestMapping("/addPatient")
	public void ADDGoods(HttpServletRequest request,HttpServletResponse response,Patient patient) throws ServletException, IOException 
	{
		patientService.addPatient(patient);
        request.getRequestDispatcher("/patientOption/lsPatient").forward(request,response);
	}
	
	//患者点击注册事件
	@RequestMapping("/beforeADD")
	public ModelAndView beforeADD()
	{
		Patient patient=patientService.findLast();
		String pID=patient.getpID();
		char[] arr=pID.toCharArray();
		StringBuilder builder = new StringBuilder();
	        for (int i = 0; i < arr.length; i++) {
//	        	如果字符的ACSLL码在48-57的范围内那肯定就是整数了
				if(arr[i] >= 48 && arr[i] <= 57) {
					builder.append(arr[i]);
				}
			}
	    Integer integer=(Integer.parseInt(builder.toString()))+1;
		String pString="P0"+integer;
		ModelAndView mav=new ModelAndView("/jsp/register.jsp");
		mav.addObject("pID",pString);
		return mav;
	}
	
	//用户注册
	@RequestMapping("/addPatient2")
	public void ADDPatient(HttpServletRequest request,HttpServletResponse response,Patient patient) throws ServletException, IOException 
	{
		patientService.addPatient(patient);
	    request.getRequestDispatcher("/jsp/patientLogin.jsp").forward(request,response);
	}
	
	//删除患者
	@RequestMapping("/delPatient/{id}")
	  public void DELGoods(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer id) throws ServletException, IOException 
	{
		patientService.delPatient(id);
	    request.getRequestDispatcher("/patientOption/lsPatient").forward(request,response); 
	}
	
	//编辑患者信息前的查询
	@RequestMapping("/editfind/{pID}")
	public ModelAndView FindById2(@PathVariable String pID)
	{
		List<Patient> list = new ArrayList<Patient>();
		Patient patient= patientService.findPatient(pID); 
		list.add(patient);
		ModelAndView mav=new ModelAndView("/jsp/pages/editPatient.jsp");
		mav.addObject("patient", list);
		mav.addObject("pID",pID);
		return mav;		
	}
	
	//编辑患者信息
	@RequestMapping("/editPatient/{id}")
	  public void EditGoods(HttpServletRequest request,HttpServletResponse response, Patient patient) throws ServletException, IOException 
	{
		patientService.editPatient(patient);
	    request.getRequestDispatcher("/patientOption/lsPatient").forward(request,response); 
	}
	
	//患者列表
	@RequestMapping("/lsPatient")
	public ModelAndView LSDoctor()
	{
		List<Patient> patientlist= patientService.LSPatient();
		ModelAndView mav=new ModelAndView();
		mav.addObject("patient",patientlist);
		mav.setViewName("/jsp/pages/patients-list.jsp");
		return mav;		
	}
	
	//登录
	@RequestMapping("/login")
	public void loginproving(HttpServletRequest request,HttpServletResponse response,Patient patient) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		Patient ad = patientService.loginproving(patient.getpName(),patient.getPassword());
		//System.out.println("调用验证方法+"+patient);
		if(ad!=null)
		{
			session.setAttribute("patient", ad);
			request.getRequestDispatcher("/jsp/index.jsp").forward(request,response); 
		}else {
			request.setAttribute("message", "账号或密码错误！请重新填写！");
			request.getRequestDispatcher("/jsp/patientLogin.jsp").forward(request,response);
		}
	}
}
